package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.BatchDeleteRequest;
import com.mes.cloud.api.dto.request.employee.*;
import com.mes.cloud.api.dto.request.position.*;
import com.mes.cloud.api.dto.response.CompartmentByEmployeeDto;
import com.mes.cloud.api.dto.response.employee.PositionListDto;
import com.mes.cloud.api.dto.response.employee.SysEmployeePositionBusinessDto;
import com.mes.cloud.api.dto.response.employee.SysEmployeePositionDto;
import com.mes.cloud.api.dto.response.position.AuthorisationTypeDto;
import com.mes.cloud.api.dto.response.position.SysPositionEmployeePageDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.BaseEmployeeDao;
import com.mes.cloud.dao.SysEmployeePositionDao;
import com.mes.cloud.dao.SysOrganizationDao;
import com.mes.cloud.dao.SysPositionDao;
import com.mes.cloud.entity.*;
import com.mes.cloud.entity.vo.employee.PositionVo;
import com.mes.cloud.entity.vo.employee.SysEmployeePositionBusinessVo;
import com.mes.cloud.entity.vo.position.AuthorisationTypeVo;
import com.mes.cloud.entity.vo.position.CompartmentByEmployeeVo;
import com.mes.cloud.entity.vo.position.SysEmployeePositionPageVo;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.CommonUtil;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户岗位
 *
 * @author xy
 * @version 2019-08-02 14:28:28
 * @email yuan.xue3@pactera.com
 */
@SuppressWarnings({"JavaDoc", "SpellCheckingInspection", "Duplicates"})
@Service
public class SysEmployeePositionService {

    private SysEmployeePositionDao sysEmployeePositionDao;

    private IPKeyGenerator ipKeyGenerator;

    private BaseEmployeeDao baseEmployeeDao;

    private LogService logService;

    private SysPositionDao sysPositionDao;

    private SysOrganizationDao sysOrganizationDao;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public SysEmployeePositionService(SysEmployeePositionDao sysEmployeePositionDao, IPKeyGenerator ipKeyGenerator, BaseEmployeeDao baseEmployeeDao, LogService logService
            , SysPositionDao sysPositionDao, SysOrganizationDao sysOrganizationDao) {
        this.sysEmployeePositionDao = sysEmployeePositionDao;
        this.ipKeyGenerator = ipKeyGenerator;
        this.baseEmployeeDao = baseEmployeeDao;
        this.logService = logService;
        this.sysPositionDao = sysPositionDao;
        this.sysOrganizationDao = sysOrganizationDao;
    }

    /**
     * 分页查列表
     *
     * @param pageRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<SysEmployeePositionDto>> pageList(SysEmployeePositionPageRequest pageRequest) {
        Page<SysEmployeePositionDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        SysEmployeePosition selectParam = new SysEmployeePosition();
        BeanUtils.copyProperties(pageRequest, selectParam);
        List<SysEmployeePosition> sysEmployeePositionList = sysEmployeePositionDao.select(selectParam);
        List<SysEmployeePositionDto> sysEmployeePositionDtoList = new ArrayList<>();
        for (SysEmployeePosition sysEmployeePosition : sysEmployeePositionList) {
            SysEmployeePositionDto sysEmployeePositionDto = new SysEmployeePositionDto();
            BeanUtils.copyProperties(sysEmployeePosition, sysEmployeePositionDto);
            sysEmployeePositionDtoList.add(sysEmployeePositionDto);
        }
        CommPager<SysEmployeePositionDto> pageResult = new CommPager<>(page, sysEmployeePositionDtoList);
        JsonResult<CommPager<SysEmployeePositionDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 查询当前登录员工岗位列表
     *
     * @return JsonResult<List < SysEmployeePositionDto>>
     */
    public JsonResult<List<SysEmployeePositionDto>> getPositionIdList() {
        //查询条件
        SysEmployeePosition selectParam = new SysEmployeePosition();
        selectParam.setEnableFlag(Constants.ENABLE_FLAG_YES);
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);
        selectParam.setEmployeeId(UserUtil.getEmployeeId());
        List<SysEmployeePosition> sysEmployeePositionList = sysEmployeePositionDao.select(selectParam);
        List<SysEmployeePositionDto> sysEmployeePositionDtoList = new ArrayList<>();
        for (SysEmployeePosition sysEmployeePosition : sysEmployeePositionList) {
            SysEmployeePositionDto sysEmployeePositionDto = new SysEmployeePositionDto();
            BeanUtils.copyProperties(sysEmployeePosition, sysEmployeePositionDto);
            sysEmployeePositionDtoList.add(sysEmployeePositionDto);
        }
        JsonResult<List<SysEmployeePositionDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysEmployeePositionDtoList);
        return jsonResult;
    }

    /**
     * 描述 员工看岗位列表
     *
     * @param positionPageListRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.tntframe.core.common.model.CommPager < com.shinho.crm.base.api.dto.response.employee.PositionListDto>>
     * @author 郑样平
     * @date 2019/8/7 11:46
     */
    public JsonResult<CommPager<PositionListDto>> positionPageList(PositionPageListRequest positionPageListRequest) {
        Page<PositionVo> page = PageHelper.startPage(positionPageListRequest.getCurrentPage(), positionPageListRequest.getPageSize());
        List<PositionVo> position = sysEmployeePositionDao.positionPageList(positionPageListRequest);
        List<PositionListDto> roleListDtoList = new ArrayList<>();
        PositionListDto positionListDto = null;
        for (PositionVo positionVo : position) {
            positionListDto = new PositionListDto();
            BeanUtils.copyProperties(positionVo, positionListDto);
            roleListDtoList.add(positionListDto);
        }
        CommPager<PositionListDto> pageList = new CommPager<>(page, roleListDtoList);
        JsonResult<CommPager<PositionListDto>> jsonResult = new JsonResult<CommPager<PositionListDto>>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageList);
        return jsonResult;
    }

    /**
     * 获取员工的所有岗位
     *
     * @param positionListRequest
     * @return
     */
    public JsonResult<List<PositionListDto>> positionList(PositionListRequest positionListRequest) {
        PositionPageListRequest positionPageListRequest = new PositionPageListRequest();
        positionPageListRequest.setEmployeeId(positionListRequest.getEmployeeId());
        List<PositionVo> position = sysEmployeePositionDao.positionPageList(positionPageListRequest);
        List<PositionListDto> roleListDtoList = new ArrayList<>();
        PositionListDto positionListDto = null;
        for (PositionVo positionVo : position) {
            positionListDto = new PositionListDto();
            BeanUtils.copyProperties(positionVo, positionListDto);
            roleListDtoList.add(positionListDto);
        }
        JsonResult<List<PositionListDto>> jsonResult = new JsonResult<List<PositionListDto>>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(roleListDtoList);
        return jsonResult;
    }

    /**
     * 描述 员工添加岗位
     *
     * @param bindPositionRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/7 10:46
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult bindPosition(BindPositionRequest bindPositionRequest) {
        JsonResult jsonResult = new JsonResult();
        //获取员工已有岗位
        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        sysEmployeePosition.setEmployeeId(bindPositionRequest.getEmployeeId());
        //未删除状态
        sysEmployeePosition.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysEmployeePosition> sysEmployeePositions = sysEmployeePositionDao.select(sysEmployeePosition);

        //新增角色剔除员工已有岗位
        List<String> idList = deleteExistPositionId(sysEmployeePositions, bindPositionRequest.getIds());

        //组装员工岗位对象
        List<SysEmployeePosition> sysEmployeePositionList = generatorSysEmployeePosition(idList, bindPositionRequest.getEmployeeId());
        if (CollectionUtils.isEmpty(sysEmployeePositionList)) {
            return jsonResult.setCode(HttpStatusEnum.REF_EXIST.getCode()).setMsg(HttpStatusEnum.REF_EXIST.getMessage());
        }
        //批量插入
        int count = sysEmployeePositionDao.batchInsert(sysEmployeePositionList);
        if (count <= 0) {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        // 如果没有主要岗位，将第一条岗位设置为主要岗位
        BaseEmployee baseEmployee = baseEmployeeDao.selectByPrimaryKey(bindPositionRequest.getEmployeeId());
        if (baseEmployee != null && StringUtils.isEmpty(baseEmployee.getMainPositionId())) {
            //第一条设置为主要岗位
            SysEmployeePosition first = sysEmployeePositionList.get(0);
            SysEmployeePosition mainPosition = new SysEmployeePosition();
            mainPosition.setId(first.getId());
            mainPosition.setPositionMainFlag(Constants.ROLE_MAINFLAG);
            sysEmployeePositionDao.updatePositionMainFlag(bindPositionRequest.getEmployeeId());
            sysEmployeePositionDao.updateByPrimaryKeySelective(mainPosition);

            BaseEmployee employUpdate = new BaseEmployee();
            employUpdate.setId(bindPositionRequest.getEmployeeId());
            // 设置主岗位
            SysPosition sysPosition = sysPositionDao.selectByPrimaryKey(first.getPositionId());
            employUpdate.setMainPositionId(sysPosition.getId());
            employUpdate.setMainPositionName(sysPosition.getName());
            // 设置主组织
            SysOrganization sysOrganization = sysOrganizationDao.selectByPrimaryKey(sysPosition.getOrganizationId());
            employUpdate.setMainOrganizationId(sysOrganization.getId());
            employUpdate.setMainOrganizationName(sysOrganization.getName());
            baseEmployeeDao.updateByPrimaryKeySelective(employUpdate);
        }

        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        this.insertLog(sysEmployeePosition.getEmployeeId(), Constants.ADD_LINK_TO_POSITION);
        return jsonResult;
    }

    /**
     * 剔除idList存在 existSysEmployeePositionList中的id
     *
     * @param existSysEmployeePositionList 数据库中已经存在的员工岗位关联关系
     * @param idList                       需要添加的角色ID
     * @return
     */
    private List<String> deleteExistPositionId(List<SysEmployeePosition> existSysEmployeePositionList, List<String> idList) {
        if (CollectionUtils.isEmpty(existSysEmployeePositionList)) {
            return idList;
        }
        List<String> existIdList = new ArrayList<>();
        //员工已经拥有的岗位ID存放到existIdList
        for (SysEmployeePosition sysEmployeePosition : existSysEmployeePositionList) {
            existIdList.add(sysEmployeePosition.getPositionId());
        }
        return deleteExistId(existIdList, idList);
    }

    /**
     * existIdList  idList做比较,返回idList中不存在existIdList中的id
     *
     * @param existIdList
     * @param idList
     * @return
     */
    private List<String> deleteExistId(List<String> existIdList, List<String> idList) {
        List<String> returnList = new ArrayList<>();
        for (String id : idList) {
            //判断id是否存在existIdList列表中
            if (!existIdList.contains(id)) {
                returnList.add(id);
            }
        }
        return returnList;
    }

    /**
     * 组装SysEmployeePosition关联数据
     *
     * @param idList     岗位ID集合
     * @param employeeId 员工ID
     * @return
     */
    private List<SysEmployeePosition> generatorSysEmployeePosition(List<String> idList, String employeeId) {
        List<SysEmployeePosition> sysEmployeeRoleList = new ArrayList<SysEmployeePosition>();
        if (CollectionUtils.isEmpty(idList)) {
            return sysEmployeeRoleList;
        }
        SysEmployeePosition sysEmployeePositionTemp = null;
        for (String id : idList) {
            sysEmployeePositionTemp = generatorSysEmployeeRole(employeeId, id);
            sysEmployeeRoleList.add(sysEmployeePositionTemp);
        }
        return sysEmployeeRoleList;
    }

    /**
     * 组织一个SysEmployeePosition
     *
     * @param employeeId
     * @param positionId
     * @return
     */
    private SysEmployeePosition generatorSysEmployeeRole(String employeeId, String positionId) {
        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        sysEmployeePosition.setId(ipKeyGenerator.getGenerateKey());
        sysEmployeePosition.setEmployeeId(employeeId);
        sysEmployeePosition.setPositionId(positionId);
        sysEmployeePosition.setUpdateDateStr(DateUtil.updateDateStr());
        sysEmployeePosition.setCreateBy(UserUtil.getCreateByName());
        sysEmployeePosition.setUpdateBy(UserUtil.getUpdateByName());
        sysEmployeePosition.setCreateByNo(UserUtil.getCreateByNo());
        sysEmployeePosition.setUpdateByNo(UserUtil.getUpdateByNo());
        return sysEmployeePosition;
    }


    /**
     * 描述  批量移除岗位
     *
     * @param batchDeleteRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/7 11:22
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult batchDelete(BatchDeleteRequest batchDeleteRequest) {
        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        sysEmployeePosition.setEmployeeId(batchDeleteRequest.getEmployeeId());
        JsonResult jsonResult = new JsonResult();
        int count = sysEmployeePositionDao.batchDelete(batchDeleteRequest.getIds());
        if (count > 0) {
            //判断当前员工下是否有关联的岗位
            SysEmployeePosition employeePosition = new SysEmployeePosition();
            employeePosition.setEmployeeId(batchDeleteRequest.getEmployeeId());
            employeePosition.setDelFlag(Constants.DEL_FLAG_NO);
            int count1 = sysEmployeePositionDao.selectCount(employeePosition);
            if(count1 == 0){
                //维护员工表的岗位组织，字段
                BaseEmployee baseEmployee = new BaseEmployee();
                baseEmployee.setId(batchDeleteRequest.getEmployeeId());
                baseEmployee.setMainPositionId("");
                baseEmployee.setMainPositionName("");
                baseEmployee.setMainOrganizationId("");
                baseEmployee.setMainOrganizationName("");
                baseEmployeeDao.updateByPrimaryKeySelective(baseEmployee);
            }
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
            this.insertLog(sysEmployeePosition.getEmployeeId(), Constants.REMOVE_LINK_TO_POSITION);
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 描述  是否主岗位
     *
     * @param employeeIsMainPositionRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/7 12:59
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult isMainPosition(EmployeeIsMainPositionRequest employeeIsMainPositionRequest) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
        jsonResult.setCode(HttpStatusEnum.FAIL.getCode());

        String id = employeeIsMainPositionRequest.getId();
        String employeeId = employeeIsMainPositionRequest.getEmployeeId();

        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        sysEmployeePosition.setId(id);
        sysEmployeePosition.setPositionMainFlag(Constants.ROLE_MAINFLAG);
        //修改 为主岗位
        sysEmployeePositionDao.updatePositionMainFlag(employeeId);
        sysEmployeePositionDao.updateByPrimaryKeySelective(sysEmployeePosition);

        //除主岗位其余修改为0
        //修改员工岗位
        BaseEmployee baseEmployee = setMainEmployeeInfo(employeeId, id);

        int count = baseEmployeeDao.updateByPrimaryKeySelective(baseEmployee);
        if (count > 0) {
            //日志
            this.insertLog(employeeIsMainPositionRequest.getEmployeeId(), Constants.SET_MAIN_POSITION);
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
            return jsonResult;
        }
        return jsonResult;

    }

    private BaseEmployee setMainEmployeeInfo(String employeeId, String id) {
        BaseEmployee baseEmployee = new BaseEmployee();
        baseEmployee.setId(employeeId);
        SysEmployeePosition employeePosition = sysEmployeePositionDao.selectByPrimaryKey(id);
        if (!StringUtils.isEmpty(employeePosition.getPositionId())) {
            SysPosition sysPosition = sysPositionDao.selectByPrimaryKey(employeePosition.getPositionId());
            baseEmployee.setMainPositionId(sysPosition.getId());
            baseEmployee.setMainPositionName(sysPosition.getName());
            //修改岗位
            if (!StringUtils.isEmpty(sysPosition.getOrganizationId())) {
                SysOrganization sysOrganization = sysOrganizationDao.selectByPrimaryKey(sysPosition.getOrganizationId());
                baseEmployee.setMainOrganizationId(sysOrganization.getId());
                baseEmployee.setMainOrganizationName(sysOrganization.getName());
            }
        }
        return baseEmployee;
    }

    /**
     * 描述 日志
     *
     * @param {sysEmployeePosition, logMessage]
     * @return int
     * @author 郑样平
     * @date 2019/8/8 9:59
     */
    private int insertLog(String employeeId, String logMessage) {
        SysEmployeeLog sysEmployeeLog = new SysEmployeeLog();
        sysEmployeeLog.setEmployeeId(employeeId);
        sysEmployeeLog.setMenuCode(Constants.EMPLOYEE_MENU_CODE);
        sysEmployeeLog.setLog(logMessage);
        sysEmployeeLog.setId(ipKeyGenerator.getGenerateKey());
        sysEmployeeLog.setUpdateDateStr(DateUtil.updateDateStr());
        sysEmployeeLog.setCreateBy(UserUtil.getCreateByName());
        sysEmployeeLog.setUpdateBy(UserUtil.getUpdateByName());
        sysEmployeeLog.setCreateByNo(UserUtil.getCreateByNo());
        sysEmployeeLog.setUpdateByNo(UserUtil.getUpdateByNo());
        return logService.insertEmployeeLog(sysEmployeeLog);
    }

    /**
     * <岗位员工列表>
     *
     * @param pageRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.tntframe.core.common.model.CommPager < com.shinho.crm.base.api.dto.response.position.SysPositionEmployeePageDto>>
     * @author xy
     * @date 2019/8/9 10:12
     */
    public JsonResult<CommPager<SysPositionEmployeePageDto>> positionEmployeePageList(SysPositionEmployeePageRequest pageRequest) {
        Page<SysPositionEmployeePageDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        SysEmployeePosition selectParam = new SysEmployeePosition();
        BeanUtils.copyProperties(pageRequest, selectParam);

        List<SysEmployeePositionPageVo> sysEmployeePositionList = sysEmployeePositionDao.positionEmployeePageList(pageRequest);

        List<SysPositionEmployeePageDto> sysEmployeePositionDtoList = new ArrayList<>();
        for (SysEmployeePositionPageVo sysEmployeePositionVo : sysEmployeePositionList) {
            SysPositionEmployeePageDto sysEmployeePositionDto = new SysPositionEmployeePageDto();
            BeanUtils.copyProperties(sysEmployeePositionVo, sysEmployeePositionDto);
            sysEmployeePositionDtoList.add(sysEmployeePositionDto);
        }
        CommPager<SysPositionEmployeePageDto> pageResult = new CommPager<>(page, sysEmployeePositionDtoList);
        JsonResult<CommPager<SysPositionEmployeePageDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * <关联员工>
     *
     * @param relationEmployeeRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author xy
     * @date 2019/8/9 10:12
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult positionRelationEmployee(RelationEmployeeRequest relationEmployeeRequest) {
        JsonResult jsonResult = new JsonResult();
        //数据重复检查
        dataRepeatCheck(relationEmployeeRequest);
        //去重后无新增数据,直接返回
        if (CollectionUtils.isEmpty(relationEmployeeRequest.getIds())) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
            return jsonResult;
        }
        String positionId = relationEmployeeRequest.getPositionId();
        List<SysEmployeePosition> list = new ArrayList<>(56);

        for (String id : relationEmployeeRequest.getIds()) {
            SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
            sysEmployeePosition.setId(ipKeyGenerator.getGenerateKey());
            sysEmployeePosition.setPositionId(positionId);
            sysEmployeePosition.setEmployeeId(id);
            sysEmployeePosition.setUpdateDateStr(DateUtil.updateDateStr());
            sysEmployeePosition.setCreateBy(UserUtil.getCreateByName());
            sysEmployeePosition.setUpdateBy(UserUtil.getUpdateByName());
            sysEmployeePosition.setCreateByNo(UserUtil.getCreateByNo());
            sysEmployeePosition.setUpdateByNo(UserUtil.getUpdateByNo());
            list.add(sysEmployeePosition);
        }
        int count = sysEmployeePositionDao.batchInsert(list);
        if (count > 0) {
            insertPositionLog(relationEmployeeRequest.getPositionId(), Constants.ADD_LINK_TO_EMPLOYEE);
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }

        return jsonResult;
    }

    /**
     * 去重复
     *
     * @param relationEmployeeRequest
     */
    private void dataRepeatCheck(RelationEmployeeRequest relationEmployeeRequest) {
        String positionId = relationEmployeeRequest.getPositionId();

        //查询岗位已关联的员工
        SysEmployeePosition queryParams = new SysEmployeePosition();
        queryParams.setPositionId(positionId);
        queryParams.setDelFlag(Constants.DEL_FLAG_NO);

        //通过岗位id查询
        List<SysEmployeePosition> sysEmployeePositionList = sysEmployeePositionDao.select(queryParams);

        if (!CollectionUtils.isEmpty(sysEmployeePositionList)) {
            Set<String> dbEmployeeIdSet = new HashSet<>();
            for (SysEmployeePosition sysEmployeePosition : sysEmployeePositionList) {
                dbEmployeeIdSet.add(sysEmployeePosition.getEmployeeId());
            }
            //请求id
            List<String> employeeIdList = relationEmployeeRequest.getIds();
            //请求 id List 转Set
            Set<String> employeeIdSet = new HashSet<>(employeeIdList);
            //去重复
            Set<String> set = CommonUtil.deleteExistId(dbEmployeeIdSet, employeeIdSet);

            List<String> employeeIds = new ArrayList<>(set);

            relationEmployeeRequest.setIds(employeeIds);
        }
    }

    /**
     * <岗位删除关联员工>
     *
     * @param relationEmployeeRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author xy
     * @date 2019/8/9 10:11
     */
    public JsonResult positionRemoveRelationEmployee(RelationEmployeeRequest relationEmployeeRequest) {
        relationEmployeeRequest.setUpdateBy(UserUtil.getUpdateByName());
        relationEmployeeRequest.setUpdateByNo(UserUtil.getUpdateByNo());
        relationEmployeeRequest.setUpdateDateStr(DateUtil.updateDateStr());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
        jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
        if (CollectionUtils.isEmpty(relationEmployeeRequest.getIds())) {
            return jsonResult;
        }

        int count = sysEmployeePositionDao.positionRemoveRelationEmployee(relationEmployeeRequest);
        if (count > 0) {
            insertPositionLog(relationEmployeeRequest.getPositionId(), Constants.REMOVE_LINK_TO_EMPLOYEE);
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        }
        return jsonResult;
    }

    /**
     * <关联员工表查询在职员工数量>
     *
     * @param positionId
     * @return int
     * @author xy
     * @date 2019/8/10 12:57
     */
    public int selectCount(String positionId) {
        return sysEmployeePositionDao.selectRelationCount(positionId);
    }

    /**
     * <插入岗位日志>
     *
     * @param positionId, logMessage
     * @return int
     * @author xy
     * @date 2019/8/12 8:38
     */
    private int insertPositionLog(String positionId, String logMessage) {
        SysPositionLog sysPositionLog = new SysPositionLog();
        sysPositionLog.setPositionId(positionId);
        sysPositionLog.setMenuCode(Constants.POSITION_MENU_CODE);
        sysPositionLog.setLog(logMessage);
        sysPositionLog.setId(ipKeyGenerator.getGenerateKey());
        sysPositionLog.setUpdateDateStr(DateUtil.updateDateStr());
        sysPositionLog.setCreateBy(UserUtil.getCreateByName());
        sysPositionLog.setUpdateBy(UserUtil.getUpdateByName());
        sysPositionLog.setCreateByNo(UserUtil.getCreateByNo());
        sysPositionLog.setUpdateByNo(UserUtil.getUpdateByNo());
        return logService.insertPositionLog(sysPositionLog);
    }

    /**
     * <岗位员工更新授权方式>
     *
     * @param sysEmployeePositionRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author xy
     * @date 2019/8/12 11:36
     */
    public JsonResult positionEmployeeUpdateAuthType(SysEmployeePositionRequest sysEmployeePositionRequest) {

        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        BeanUtils.copyProperties(sysEmployeePositionRequest, sysEmployeePosition);
        int count = sysEmployeePositionDao.updateByPrimaryKeySelective(sysEmployeePosition);
        JsonResult jsonResult = new JsonResult();
        if (count > 0) {
            insertPositionLog(sysEmployeePosition.getPositionId(), Constants.SET_POSITION_TYPE);
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 同步数据-增加
     *
     * @param synchroPositionEmployeeAddRequest
     * @return
     */
    public JsonResult synchroAdd(SynchroPositionEmployeeAddRequest synchroPositionEmployeeAddRequest) {
        JsonResult jsonResult = new JsonResult();
        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        BeanUtils.copyProperties(synchroPositionEmployeeAddRequest, sysEmployeePosition);
        sysEmployeePosition.setId(ipKeyGenerator.getGenerateKey());
        sysEmployeePosition.setUpdateDateStr(DateUtil.updateDateStr());
        sysEmployeePosition.setCreateBy(UserUtil.getCreateByName());
        sysEmployeePosition.setUpdateBy(UserUtil.getUpdateByName());
        sysEmployeePosition.setCreateByNo(UserUtil.getCreateByNo());
        sysEmployeePosition.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysEmployeePositionDao.insertSelective(sysEmployeePosition);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 同步数据-更新员工岗位
     *
     * @param synchroPositionEmployeeUpdateByPositionIdAndEmployeeIdRequest
     * @return
     */
    public JsonResult updateByPositionIdAndEmployeeId(SynchroPositionEmployeeUpdateByPositionIdAndEmployeeIdRequest synchroPositionEmployeeUpdateByPositionIdAndEmployeeIdRequest) {
        JsonResult jsonResult = new JsonResult();
        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        BeanUtils.copyProperties(synchroPositionEmployeeUpdateByPositionIdAndEmployeeIdRequest, sysEmployeePosition);
        sysEmployeePosition.setUpdateDateStr(DateUtil.updateDateStr());
        sysEmployeePosition.setUpdateBy(UserUtil.getUpdateByName());
        sysEmployeePosition.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysEmployeePositionDao.updateByPositionIdAndEmployeeId(sysEmployeePosition);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 同步数据-记录总数
     *
     * @param synchroPositionEmployeeCountByPositionIdAndEmployeeIdRequest
     * @return
     */
    public JsonResult synchroCountByPositionIdAndEmployeeId(SynchroPositionEmployeeCountByPositionIdAndEmployeeIdRequest synchroPositionEmployeeCountByPositionIdAndEmployeeIdRequest) {
        JsonResult jsonResult = new JsonResult();
        SysEmployeePosition sysEmployeePosition = new SysEmployeePosition();
        sysEmployeePosition.setPositionId(synchroPositionEmployeeCountByPositionIdAndEmployeeIdRequest.getPositionId());
        sysEmployeePosition.setEmployeeId(synchroPositionEmployeeCountByPositionIdAndEmployeeIdRequest.getEmployeeId());
        sysEmployeePosition.setDelFlag(Constants.DEL_FLAG_NO);
        int count = sysEmployeePositionDao.selectCount(sysEmployeePosition);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(count);

    }

    public JsonResult<List<SysEmployeePositionBusinessDto>> getEmployeeByPositionId(SysEmployeePositionBusinessRequest req) {

        List<SysEmployeePositionBusinessVo> sysEmployeePositionList = sysEmployeePositionDao.getEmployeeByPositionId(req);

        List<SysEmployeePositionBusinessDto> sysEmployeePositionDtoList = new ArrayList<>();
        for (SysEmployeePositionBusinessVo sysEmployeePosition : sysEmployeePositionList) {
            SysEmployeePositionBusinessDto sysEmployeePositionDto = new SysEmployeePositionBusinessDto();
            BeanUtils.copyProperties(sysEmployeePosition, sysEmployeePositionDto);
            sysEmployeePositionDtoList.add(sysEmployeePositionDto);
        }
        JsonResult<List<SysEmployeePositionBusinessDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysEmployeePositionDtoList);
        return jsonResult;
    }

    /**
     * 查询当前岗所有上级岗位及岗位人员信息
     *
     * @param req
     * @return JsonResult<List < SysEmployeePositionBusinessDto>>
     */
    public JsonResult<List<SysEmployeePositionBusinessDto>> getAllParentEmployeeByPositionId(SysEmployeePositionBusinessRequest req) {

        List<SysEmployeePositionBusinessVo> sysEmployeePositionList = sysEmployeePositionDao.getAllParentEmployeeByPositionId(req);

        List<SysEmployeePositionBusinessDto> sysEmployeePositionDtoList = new ArrayList<>();
        for (SysEmployeePositionBusinessVo sysEmployeePosition : sysEmployeePositionList) {
            SysEmployeePositionBusinessDto sysEmployeePositionDto = new SysEmployeePositionBusinessDto();
            BeanUtils.copyProperties(sysEmployeePosition, sysEmployeePositionDto);
            sysEmployeePositionDtoList.add(sysEmployeePositionDto);
        }
        JsonResult<List<SysEmployeePositionBusinessDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysEmployeePositionDtoList);
        return jsonResult;
    }

    /**
     * 查询子级岗位员工信息
     *
     * @param req
     * @return JsonResult<List < SysEmployeePositionBusinessDto>>
     */
    public JsonResult<List<SysEmployeePositionBusinessDto>> getChildrenEmployeeByPositionId(SysEmployeePositionBusinessRequest req) {
        List<SysEmployeePositionBusinessVo> sysEmployeePositionList = sysEmployeePositionDao.getChildrenEmployeeByPositionId(req);

        List<SysEmployeePositionBusinessDto> sysEmployeePositionDtoList = new ArrayList<>();
        for (SysEmployeePositionBusinessVo sysEmployeePosition : sysEmployeePositionList) {
            SysEmployeePositionBusinessDto sysEmployeePositionDto = new SysEmployeePositionBusinessDto();
            BeanUtils.copyProperties(sysEmployeePosition, sysEmployeePositionDto);
            sysEmployeePositionDtoList.add(sysEmployeePositionDto);
        }
        JsonResult<List<SysEmployeePositionBusinessDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysEmployeePositionDtoList);
        return jsonResult;
    }


    /**
     * 描述 岗位下有多个员工 去一个 做为业务代表的主要员工
     *
     * @param positionId
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.crm.base.api.dto.response.CompartmentByEmployeeDto>
     * @author 郑样平
     * @date 2019/11/6 16:37
     */
    public JsonResult<CompartmentByEmployeeDto> getCompartmentByEmployee(String positionId) {
        JsonResult jsonResult = new JsonResult();
        CompartmentByEmployeeVo compartmentByEmployeeVo = sysEmployeePositionDao.getCompartmentByEmployee(positionId);
        if (compartmentByEmployeeVo == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        CompartmentByEmployeeDto compartmentByEmployeeDto = new CompartmentByEmployeeDto();
        BeanUtils.copyProperties(compartmentByEmployeeVo, compartmentByEmployeeDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(compartmentByEmployeeDto);
    }

    public JsonResult<List<PositionListDto>> queryPositionList(List<String> positionIds) {
        List<PositionVo> position = sysEmployeePositionDao.queryPositionList(positionIds);
        List<PositionListDto> roleListDtoList = new ArrayList<>();
        for (PositionVo positionVo : position) {
            PositionListDto positionListDto = new PositionListDto();
            BeanUtils.copyProperties(positionVo, positionListDto);
            roleListDtoList.add(positionListDto);
        }
        JsonResult<List<PositionListDto>> jsonResult = new JsonResult<List<PositionListDto>>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(roleListDtoList);
        return jsonResult;
    }

    public JsonResult<List<String>> queryPositionIdList(SysEmployeePositionRequest sysEmployeePositionRequest) {
        //查询条件
        SysEmployeePosition selectParam = new SysEmployeePosition();

        BeanUtils.copyProperties(sysEmployeePositionRequest, selectParam);
        List<String> positionIds = sysEmployeePositionDao.queryPositionIdList(selectParam);

        JsonResult<List<String>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(positionIds);
        return jsonResult;
    }

    /**
     * 描述 :当前登录员工，主要岗位的个人授权和组织授权
     *
     * @param
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.crm.base.api.dto.response.position.AuthorisationTypeDto>
     * @author 郑样平
     * @date 2019/11/26 13:54
     */
    public JsonResult<AuthorisationTypeDto> getAuthorisationType() {
        //获取当前员工
        String employeeId = UserUtil.getEmployeeId();
        AuthorisationTypeVo authorisationTypeVo = sysEmployeePositionDao.selectAuthorisationType(employeeId);
        AuthorisationTypeDto authorisationTypeDto = new AuthorisationTypeDto();
        if (authorisationTypeVo != null) {
            BeanUtils.copyProperties(authorisationTypeVo, authorisationTypeDto);
        }
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(authorisationTypeDto);
    }
}
